﻿--[[
   @brief    feedback_frame_view 
   @author   perpu
   @version  1.0
   @date     2017-03-21
--]]

feedback_frame_view = 
{
    pDialog_ = nil;
    dwRootId_ = 0;
    last_url_ = nil;
    is_dev_client_ = false;
}

function feedback_frame_view:init()
    base_utility.log("[feedback_frame_view] init ");
    local game_lib = base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
    self.is_dev_client_ = game_lib ~= nil and game_lib:Is_Develop_ver();
end

function feedback_frame_view:navigate(ext_param)
    base_utility.log("[feedback_frame_view] navigate ");
    if self:is_created() then
        self:show(true);
        self:re_open_web_url(ext_param);
        return;
    end
    if self:create(ext_param) == true then
        self:re_open_web_url(ext_param);
    else
        base_utility.log("[feedback_frame_view][show]failed to create dialog", true);
    end
end

function feedback_frame_view:show(bShow)
    base_utility.log("[feedback_frame_view] show " .. tostring(bShow));
    if self.pDialog_ then
        if bShow then
            self.pDialog_:Show(TRUE);
            self.pDialog_:Restore();
            self.pDialog_:BringToTop();
            self:set_web_focus();
        else
            self.pDialog_:Show(FALSE);
        end
    else
        base_utility.log("[feedback_frame_view][show]dialog not exist");
    end
end

function feedback_frame_view:is_created()
    if self.pDialog_ then
        return true;
    else
        return false;
    end
end

function feedback_frame_view:is_show()
    local showed = false;
    if self.pDialog_ then
        if self.pDialog_:IsVisible() == TRUE then
            showed = true;
        end
    end
    return showed;
end

function feedback_frame_view:re_open_web_url(ext_param)
    base_utility.log("[feedback_frame_view]re_open_web_url, jumpfrom=" .. tostring(ext_param.jumpfrom));
    local url = "";
    if self.is_dev_client_ then
        url = LoadStr("FEEDBACK_FORM_URL").. "win_id=" .. tostring(self.dwRootId_) ..
                "&tgpid="..tostring(base_utility.get_tgp_id())..
                "&gameid="..tostring(ext_param.gameid)..
                "&jumpfrom="..tostring(ext_param.jumpfrom);
        url = string.gsub(url, "jumpfrom=%w", "jumpfrom=developer");
    else
        url = LoadStr("HELPFEEDBACK_URL").. "win_id=" .. tostring(self.dwRootId_) ..
                "&tgpid="..tostring(base_utility.get_tgp_id())..
                "&gameid="..tostring(ext_param.gameid)..
                "&jumpfrom="..tostring(ext_param.jumpfrom);
    end

    if url == self.last_url_ then
        return false;
    end
    if self.dwRootId_ ~= 0 then
        self.pDialog_ = ui_utility.get_ctrl_ptr_by_id(self.dwRootId_);
        local pWebCtrl = ui_utility.get_ctrl_ptr_by_cid(CID_WEB_CTRL_FOR_FEEDBACK);

        local social =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        local webkit_switch = social:get_webkit_switch();
        self.last_url_ = url;
        local dst_url = web_utility.make_login_status_url(url, login_model.main_uin);
        local ui_str, str_holder = U8toCUIStr(dst_url);
        if pWebCtrl then
            pWebCtrl:Navigate(ui_str);
        end
        return true;
    else
        base_utility.log("[feedback_frame_view] feedback_frame_view error!!!");
        return false;
    end
end


function feedback_frame_view:set_web_focus()
    local pWebCtrl = ui_utility.get_ctrl_ptr_by_cid(CID_WEB_CTRL_FOR_FEEDBACK);
    pWebCtrl:SetFocus();
end

function feedback_frame_view:create(ext_param)
    if self.pDialog_ then
        feedback_frame_model.is_already_exist = true;
        return true;
    end

    self.dwRootId_ = layout_mgr.restore_layout("feedback.tpf");
    
    if self.dwRootId_ ~= 0 then
        self.pDialog_ = ui_utility.get_ctrl_ptr_by_id(self.dwRootId_);
        local pWebCtrl = ui_utility.get_ctrl_ptr_by_cid(CID_WEB_CTRL_FOR_FEEDBACK);
        ----shadow_helper.create_shadow(self.dwRootId_);
        local social =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        local webkit_switch = social:get_webkit_switch();
        local url = "";
        if self.is_dev_client_ then
            url = LoadStr("FEEDBACK_FORM_URL").."tgpid="..tostring(base_utility.get_tgp_id())..
                "&gameid="..tostring(ext_param.gameid)..
                "&jumpfrom="..tostring(ext_param.jumpfrom);
            url = string.gsub(url, "jumpfrom=%w", "jumpfrom=developer");
        else
            url = LoadStr("HELPFEEDBACK_URL").."tgpid="..tostring(base_utility.get_tgp_id())..
                    "&gameid="..tostring(ext_param.gameid)..
                    "&jumpfrom="..tostring(ext_param.jumpfrom);
        end
        self.last_url_ = url;
        local dst_url = web_utility.make_login_status_url(url, login_model.main_uin);
        local ui_str, str_holder = U8toCUIStr(dst_url);
        if pWebCtrl then
            pWebCtrl:Navigate(ui_str);
            pWebCtrl:SetFocus();
        end
        return true;
    else
        base_utility.log("[feedback_frame_view] feedback_frame_view error!!!");
        return false;
    end
end

function feedback_frame_view:hide(uilib, ctrl_id)
    --shadow_helper.destroy_shadow(self.dwRootId_);
    base_utility.log("[feedback_frame_view] remove feedback_frame_view tpf");
    layout_mgr.delay_remove_ctrl(self.dwRootId_);
    self.dwRootId_ = 0;
    self.pDialog_ = nil;
end

function feedback_frame_view:hide_close_btn()
    base_utility.log("[feedback_frame_view] hide_close_btn");
    ui_utility.show_child(self.dwRootId_, "TitleBarCtn\\Btn_close", false);
end

function feedback_frame_view:on_set_focuse(uilib, ctrl_id)
    self.set_web_focus();
end

function feedback_frame_view:close()
    self.pDialog_:Show(FALSE);
end

function feedback_frame_view:get_window()
    if self.pDialog_ ~= nil then
        return self.pDialog_:GetHwnd();
    end

    return 0;
end

function feedback_frame_view:set_focus_dialog()
    base_utility.log("[feedback_frame_view] set_focus_dialog feedback_frame_view tpf");
    if self.pDialog_ ~= nil then
        self.pDialog_:SetFocus();
    end
end

feedback_frame_view:init();

